package com.patrol.logisticsmanage.ui.orders;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.patrol.logisticsmanage.R;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

public class OrderDetailAdapter extends RecyclerView.Adapter<OrderDetailAdapter.ViewHolder> {

    // 普通的item ViewType
    private static final int TYPE_ITEM = 1;
    // 空布局的ViewType
    private static final int TYPE_EMPTY = 2;

    private Context mContext;

    // 数据
    private List<OrderDetail> mList;

    // 是否显示空布局，默认不显示
    private boolean showEmptyView = false;

    public OrderDetailAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_EMPTY) {
            // 创建空布局item
            return new ViewHolder(getEmptyView(parent));
        } else {
            // 创建普通的item
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_detail, parent, false);
            return new ViewHolder(view);
        }
    }

    /**
     * 获取空布局
     */
    private View getEmptyView(ViewGroup parent) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.empty_page, parent, false);
        return view;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 如果是空布局item，不需要绑定数据
        if (!isEmptyPosition(position)) {
            OrderDetail detail = mList.get(position);
            ((TextView)holder.cardView.findViewById(R.id.food_name)).setText(detail.getXfMeNa() + " ¥" + detail.getXfPirce());
            ((TextView)holder.cardView.findViewById(R.id.yingyang)).setText("热量(千卡):" + detail.getRel() + "|蛋白质(克):" + detail.getDanbz()+ "|脂肪(克):"+detail.getZhif() +
                    "\n碳水化物:" + detail.getTanshhw()+ "|胆固醇:" + detail.getDangc() + "|盐:" + detail.getNa() + "|钙:" + detail.getGai());
            ImageView imageView = ((ImageView)holder.cardView.findViewById(R.id.food_img)); //.setImageDrawable( LoadImageFromWebOperations(detail.getCaiptp()));
            Handler handler = new Handler();
            new Thread(()->{
                Drawable drawable = LoadImageFromWebOperations(detail.getCaiptp());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageDrawable(drawable);
                    }
                });
            }).start();
//            holder.cardView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    ARouter.getInstance().build("/app/orderDetails").withString("outid",o.getOutid()).navigation();
//                }
//            });
//            holder.cardView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
//            holder.cardView.set
        }
    }

    @Override
    public int getItemCount() {
        // 判断数据是否空，如果没有数据，并且需要显示空布局，就返回1。
        int count = mList != null ? mList.size() : 0;
        if (count > 0) {
            return count;
        } else if (showEmptyView) {
            // 显示空布局
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isEmptyPosition(position)) {
            // 空布局
            return TYPE_EMPTY;
        } else {
            return TYPE_ITEM;
        }
    }

    public void setList(List<OrderDetail> list) {
        mList = list;
        notifyDataSetChanged();
    }

    /**
     * 判断是否是空布局
     */
    public boolean isEmptyPosition(int position) {
        int count = mList != null ? mList.size() : 0;
        return position == 0 && showEmptyView && count == 0;
    }

    /**
     * 设置空布局显示。默认不显示
     */
    public void showEmptyView(boolean isShow) {
        if (isShow != showEmptyView) {
            showEmptyView = isShow;
            notifyDataSetChanged();
        }
    }

    public boolean isShowEmptyView() {
        return showEmptyView;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        CardView cardView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.order_detail_item);
        }
    }

    private Drawable LoadImageFromWebOperations(String url)
    {
        try
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exc="+e);
            return null;
        }
    }

}